package cn.xval.ft.app.gen.service.impl;

import cn.xval.fc.base.exception.Asserts;
import cn.xval.fc.base.exception.BusinessException;
import cn.xval.fc.core.security.TokenContext;
import cn.xval.ft.app.gen.database.entity.FrontHost;
import cn.xval.ft.app.gen.database.repository.FrontHostRepository;
import cn.xval.ft.app.gen.params.FrontHostParams;
import cn.xval.ft.app.gen.service.FrontHostService;
import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class FrontHostImpl extends TokenContext implements FrontHostService {
    @Resource
    private FrontHostRepository repoFrontHost;
    @Override
    public FrontHostParams.FrontHostInfo create(FrontHostParams.FrontHostCreateRequest arg) throws BusinessException {
        var entity = new FrontHost();
        entity.setFrontType(arg.getFrontType());
        entity.setResolveType(arg.getResolveType());
        entity.setProtocol(arg.getProtocol());
        entity.setPort(arg.getPort());
        entity.setAddr(arg.getAddr());
        entity.setName(arg.getName());
        this.repoFrontHost.save(entity);
        return translate2Info(entity);
    }
    @Override
    public FrontHostParams.FrontHostInfo modify(FrontHostParams.FrontHostModifyRequest arg) throws BusinessException {
        var entityId = decryptLong(arg.getId());
        var entityOpt = this.repoFrontHost.findById(entityId);
        Asserts.Business.withStatusText("记录不存在").assertTrue(entityOpt.isPresent(), "记录不存在: %s", entityId);
        var entity = entityOpt.get();
        entity.setName(arg.getName());
        entity.setAddr(arg.getAddr());
        entity.setProtocol(arg.getProtocol());
        entity.setPort(arg.getPort());
        entity.setResolveType(arg.getResolveType());
        entity.setFrontType(arg.getFrontType());
        this.repoFrontHost.save(entity);
        return translate2Info(entity);
    }
    @Override
    public void remove(String id) throws BusinessException {
        this.repoFrontHost.deleteById(decryptLong(id));
    }
    @Override
    public List<FrontHostParams.FrontHostInfo> query() throws BusinessException {
        var sortInfo = Sort.by(Sort.Order.asc("name"));
        var list = this.repoFrontHost.findAll(sortInfo);
        var results = new ArrayList<FrontHostParams.FrontHostInfo>();
        for (var ent: list) {
            results.add(translate2Info(ent));
        }
        return results;
    }
    public FrontHostParams.FrontHostInfo translate2Info(FrontHost arg) throws BusinessException {
        var info = new FrontHostParams.FrontHostInfo();
        info.setId(encrypt(arg.getId()));
        info.setName(arg.getName());
        info.setFrontType(arg.getFrontType());
        info.setResolveType(arg.getResolveType());
        info.setProtocol(arg.getProtocol());
        info.setAddr(arg.getAddr());
        info.setPort(arg.getPort());
        return info;
    }
}

